/* 
 *  constante do sistema DOMINIOADMIN = url da aplicacao mais o /admin
    funcao issetElement() e uma funcao so sistema que pode ser chamada em qualque modulo
    funcao swal() e uma fucao do sistema ser utilizada em qualquer modulo
    funcao post_requests() funcao do sistema para fazer requisicoes via post
 */




function deleteTab(e,lc){
    var $_this = jQuery(e),data = $_this.data(), url = DOMINIOADMIN+"tabs/delete";
    swal({
        type  : 'warning',
        title : "Atenção",
        text  : "Deseja mesmo exluir este item?",
        showCancelButton:true,
        confirmButtonText:"Sim, desejo",
	cancelButtonText:"Nao, foi um engano",
        confirmButtonColor:"#EC5E67",
        closeOnConfirm: false
    },function(){
        post_requests(url,{id:data.id,table:data.table},function(res){
            var $_obj = jQuery.parseJSON(res);
            if($_obj.status){
                optionsAlert = {type:"success", title:'Sucesso', text:$_obj.msg};
            }else{
                optionsAlert = {type:"error", title:"Error", text:$_obj.msg}
            }
            swal(optionsAlert,function(){
                if(lc !==undefined){
                    window.location.href=DOMINIOADMIN+"tabs/edit/"+lc;
                }else{
                    window.location.reload();
                }
            });
            
        });
    });
    
}


jQuery(function(){
    
    
    
    if(issetElement("#frm_add_tab_shortcode")){
        //exclui aba MAIN
        jQuery(document).on("click",".deltabs",function(){
            deleteTab(this);
        });
        
        //deixa sempre vazio o campo input        
        jQuery(document).on("open",".remodal",function(){
            jQuery("input#registro_tabs").val('');
        });
        
        jQuery(document).on("close",".remodal",function(e){
             console.log("close");
            jQuery("#loadProcessTab").hide('normal',function(){
                jQuery(this).find("i").removeClass("ion-alert-circled").addClass("ion-load-c fa-spin").next("em").text("Por favor aguarde...");
            })
            jQuery("input#registro_tabs").val('');
        });
        
        jQuery("#frm_add_tab_shortcode").ajaxForm({
            beforeSubmit:function(){
                jQuery("#loadProcessTab").find("i").removeClass("ion-alert-circled").addClass("ion-load-c fa-spin").next("em").text("Por favor aguarde...");
                jQuery("#loadProcessTab").fadeIn();
            },
            success:function(resp){
                var $_icon = jQuery("#loadProcessTab").find("i"),
                    $_obj = jQuery.parseJSON(resp);
                if($_obj.status){
                    var ins = $.remodal.lookup[$("[data-remodal-id='frm_create']").data('remodal')];
                    ins.close();
                    jQuery("input#registro_tabs").val('')
                    var tr = "<tr>";
                        tr+="<td>"+$_obj.title+"</td>";
                        tr+="<td>"+$_obj.shorcode+"</td>";
                        tr+="<td>";
                        tr+="<div class='btn-group'>";
                        tr+="<a class='btn btn-primary' href='"+DOMINIOADMIN+"tabs/edit/"+$_obj.id+"' ><i class='el el-edit'></i> Editar</a>";
                        tr+="<a class='btn btn-danger deltabs' href='javascript:;' data-id='"+$_obj.id+"' data-table='cms_tabs'><i class='el el-trash'></i> Apagar</a> ";
                        tr+="<div>";
                        tr+="</td>";
                        tr+="</td>";
                    jQuery('#tabs_list').prepend(tr);
                }else{
                    $_icon.removeClass('ion-load-c fa-spin').addClass('ion-alert-circled').next('em').text($_obj.msg);
                }               
            }
        })
    }
   
    if(issetElement('#frm_add_tabs')){
        //plugin CKEDITOR
        CKEDITOR.replace('conteudo_tabs');
        
        var options = {
             beforeSubmit:function(){
                 boxMensageLoad("aguarde por favor","fa fa-loader",true);
             },
             beforeSerialize:function(){
                for(instance in CKEDITOR.instances){
                    CKEDITOR.instances[instance].updateElement();
                }
            },
            success:function(resp){
                var $_obj = jQuery.parseJSON(resp);
                if($_obj.status){
                    boxMensageLoad($_obj.msg,"fa fa-check",false);
                    setTimeout(function(){
                        window.location.reload();
                    },2000);                  
                }else{
                    boxMensageLoad($_obj.msg,"fa fa-warning",false);
                }
            }
        };
        jQuery("#frm_add_tabs").ajaxForm(options);
        
        ///easy tabs
        var $_btnEditTabPost = jQuery("#btn_tabpost_edit");
        $("#tabs_preview").easyResponsiveTabs({
            type: 'horizontal',
            tabidentify: 'hor_1',
            activate:function(e){
                var $_li  = jQuery(e.target),
                    value = $_li.data('id'),
                    tabIndex = $_li.index(),
                    url   = DOMINIOADMIN+"tabs/edit/"+$_btnEditTabPost.data('tab_id_main')+"/"+value+"#tabs_preview"+(tabIndex+1);
                $_btnEditTabPost.show().attr('href',url);
            }
        });
        
        
        //excluir tab posts
        jQuery(document).on("click",".deltabs",function(){
            deleteTab(this,jQuery(this).attr('rel'));
        });
    }
   
   
});
